define(['../carrys/swiper','../../../js/lib/other/sweet-alert'],function() {
    return ['$scope','$location', '$interval','$timeout','bizProxy', function($scope,$location,$interval,$timeout,bizProxy) {
    	$scope.$emit("top-show",'false');//顶部隐藏
    	$scope.$emit('menuTypeLight','home_page'); 	//控制底部menu高亮
    	$scope.$emit('bub-menu','false'); 	//显示底部menu
    	
    	$timeout(function(){$scope.htmlStru = '#home_page .scrollTempCase';},0);
    	NPLCacheData.firstStepData = null;
    	NPLCacheData.secondStepData = null;
    	NPLCacheData.thirdStepData = null;
    	bodyInfo.loanFormData = "";
        var mySwiper;
        if(transData.scrollCaseList){
             $scope.loadingShow = false;
             $scope.tempCaseData = transData.scrollCaseList;
             swiperImage();
        }else{
            getProxyData();
        }

    	//监听刷新状态
    	$scope.$on('caselibRefreshStep1', function(event, data) {
            transData.scrollCaseList = "";
            try {
                mySwiper.destroy();
            } catch(e) {
                // statements
                console.log(e);
            }
            
            getProxyData();
        });
        
        //立即贷款
        $scope.quickLoan = function(){
        	//判断有没有登录
    		if(transData.sessionId != null && transData.sessionId != ""){
    			//判断是不是律师
    			if(transData.userType === '1'){
    				$scope.$emit("goNext","/applyRiskInvest");
				}else{
					FIREFLY("对不起，您还不是律师用户", "请先在'个人信息'中升级为律师用户！","",function(){	});
				}
    		}else{
    			$scope.$emit("goNext","/login");
    		}
        }
        
        //了解贷款
        $scope.comprehendLoan = function(){
        	//了解风险投
        	$scope.$emit('goNext','/realRiskInvest');
        }


        //点击了解更多
        $scope.goKnowMore = function(flag){
            if(flag == 'diagRealize'){
                $scope.$emit("goNext","/diagRealize");
            }else if(flag == 'lawsuitRealize'){
                $scope.$emit("goNext","/lawsuitRealize");
            }else if(flag == 'caseRealize'){
                $scope.$emit("goNext","/caseRealize");
            }else if(flag == 'creditorRealize'){
                $scope.$emit("goNext","/creditorRealize");
            }
        }
        
        //立即结案
        $scope.quickIntake = function(){
        	//判断有没有登录
    		if(transData.sessionId != null && transData.sessionId != ""){
    			//判断是不是律师
    			if(transData.userType === '1'){
    				$scope.$emit("goNext","/applyNpl");
				}else{
					FIREFLY("对不起，您还不是律师用户", "请先在'个人信息'中升级为律师用户！","",function(){	});
				}
    		}else{
    			$scope.$emit("goNext","/login");
    		}
        }
        
        //发布案件
        $scope.issueCase = function(){
        	//了解不良资产
        	$scope.$emit('goNext','/realNpl');
        }
        $scope.applyRisk = function(){
            if(transData.sessionId != null && transData.sessionId != ""){
                if(resultData.loginUserData.userName){
                    $scope.$emit("goNext","/applyRisk");
                }else{//未完善资料用户不可申请
                    FIREFLY("请先去'我的'里面完善个人信息", "","",function(){});
                }
            }else{
                $scope.$emit("goNext","/login");
            }
        }
        
        //点击去免息贷
        $scope.goLoan = function(){
        	$scope.$emit('goNext',"/fireflyLoan");
        }

        
        //点击去案源库
        $scope.caseSrcLib = function(){
        	$scope.$emit('goNext',"/caseSrcLib/1");
        }
        
        
        //自动登录
        function autoLogin(){
    		var headerInfo = {},bizInfo={};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = '';
        	
        	bizInfo.loginType = 'autoLogin';
        	bizInfo.token = localStorage.getItem("fireFlySessionId");
        	//如果本地有上次登陆的记录，开始登陆
           	if(! bizInfo.token){
//           		bizInfo.token = "";
           		//如果本地没有token不再自动登陆
           		$timeout(function(){
          			//$scope.$emit("goNext","/login");
                    queryDdVersionList();
	    	   		return;
    	   		},0);
           		return;
           	}
           	bizProxy.queryProxyBizData("userLogin",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
    					console.log(tempData)
    					localStorage.setItem("fireFlySessionId",tempData.sessionId+"");
                        var body = JSON.parse(tempData.body);
                        resultData.loginResultData = JSON.parse(tempData.body);
						transData.mobileNo = body.mobileNo;
						transData.sessionId = tempData.sessionId;
						transData.userId = body.userId;
						transData.userType = body.userType;
                        queryUserExt()
                        transData.isLoadOk=true;
		    		},function(data){//FAIL
                    console.log(data)
                    transData.isLoadOk=true;
	    				if(data != ""){
	    					if(data.errorCode == ERROR_CODE.notExist){
	    						$scope.$emit("promptAlertMsg",data.errorMsg);
	    						$scope.$emit("showCollect",true);
	    						$timeout(function(){
	    							$scope.$emit("showCollect",false);
	    							//$scope.$emit("goNext","/login");
	    						},transData.promptTime);
							}else if(data.errorCode == ERROR_CODE.pwdNotMatch){
								$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									//$scope.$emit("goNext","/login");
								},transData.promptTime);
							}else if(data.errorCode == ERROR_CODE.errorToken){
								/*$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									//$scope.$emit("goNext","/login");
								},transData.promptTime);*/
							}else{
								/*$scope.$emit("promptAlertMsg","连接超时");
								$scope.$emit("showCollect",true);
								$timeout(function(){
									$scope.$emit("showCollect",false);
									//$scope.$emit("goNext","/login");
								},transData.promptTime);*/
							}
	    				}else{
	    					/*$scope.$emit("loadingChangeTpr",false);
	    					$scope.$emit("promptAlertMsg","连接超时");
	    					$scope.$emit("showCollect",true);
	    					$timeout(function(){
	    						$scope.$emit("showCollect",false);
	    						//$scope.$emit("goNext","/login");
	    					},transData.promptTime);*/
	    				}
						
		    		});
    	}

		//查询用户扩展信息
        function queryUserExt(){
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = transData.sessionId;
            $scope.$emit("loadingChange",true);
            bizProxy.queryProxyBizData("queryUserExt",headerInfo,bizInfo,
                function(tempData){//SUCCESS
                    console.log("查询用户信息=",tempData);
                    $scope.$emit("loadingChange",false);
                    $timeout(function(){
                        var body = JSON.parse(tempData.body);
                        transData.userType=body.userType;
                        resultData.loginUserData=body;
                    },100);
                    queryDdVersionList()
                },function(data){
                	console.log(data);
                    if(data != ""){//FAIL
                        $scope.$emit("loadingChange",false);
                        if(data.errorCode == ERROR_CODE.noLogin){
                            FIREFLY("", data.errorMsg,"",function(){
                                //调用退出登录
                                transData.mobileNo = "";
                                transData.password = "";
                                transData.sessionId = "";
                                transData.userType = "";
                                resultData.loginUserData = "";
                                $timeout(function(){
                                    $scope.$emit("goNext","/mine");
                                },0);
                            });
                        }else{
                            // $scope.$emit("promptAlertMsg",data.errorMsg);
                            // $scope.$emit("showCollect",true);
                            // $timeout(function(){
                            //     $scope.$emit("showCollect",false);
                            // },transData.promptTime);
                        }
                    }else{
                        $scope.$emit("loadingChange",false);
                        $scope.$emit("promptAlertMsg","连接超时");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                        },transData.promptTime);
                    }
                });
        }

        function queryDdVersionList(){
            if(transData.stl_baseData){
                return;
            }else {
                transData.stl_baseData=true;
            }
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = transData.sessionId;
            bizProxy.queryProxyBizData("queryDdVersionList", headerInfo,bizInfo,function(tempData){
                if(tempData.body != '{}'){
                    console.log("数字字典版本号",JSON.parse(tempData.body));
                    $timeout(function(){
                        var list = JSON.parse(tempData.body).ddVersionList;
                        var storage_DdVersionList = localStorage.getItem("storage_DdVersionList");
                        if(storage_DdVersionList){
                        	try{
                        		var cacheDdVersionData = JSON.parse(storage_DdVersionList);
                                var arr = [];
                                for (var i = 0; i < list.length; i++) {
                                    for (var j = 0; j < cacheDdVersionData.length; j++) {
                                        if (list[i].ddName == cacheDdVersionData[j].ddName) {
                                            if (parseInt(list[i].versionNo) > parseInt(cacheDdVersionData[j].versionNo)) {
                                                arr.push(list[i].ddName==='caseType'?'queryCaseTypeList':list[i].ddName==='caseStage'?'queryCaseStageList':list[i].ddName==='fundUse'?'queryFundUseList':list[i].ddName==='advField'?'queryAdvFieldList':'queryDocKindList')
                                            }
                                        }
                                    }
                                }
                                localStorage.setItem("storage_DdVersionList", JSON.stringify(list));
                                if(arr.length>0){
                                    getBaseData(arr)
                                }
                        	}catch(e){
                        		$timeout(function(){
                                    localStorage.setItem("storage_DdVersionList",JSON.stringify(list));
                                    getBaseData(['queryCaseTypeList','queryCaseStageList','queryFundUseList','queryDocKindList','queryAdvFieldList'])
                                },0);
                        	}
                        }else {
                            $timeout(function(){
                                localStorage.setItem("storage_DdVersionList",JSON.stringify(list));
                                getBaseData(['queryCaseTypeList','queryCaseStageList','queryFundUseList','queryDocKindList','queryAdvFieldList'])
                            },0);
                        }
                    },0);
                }
            },function(data){
                if(data.errorMsg){
                    // $scope.$emit("loadingChange",false);
                    // $scope.$emit("promptAlertMsg",data.errorMsg);
                    // $scope.$emit("showCollect",true);
                    // $timeout(function(){
                    //     $scope.$emit("showCollect",false);
                    // },transData.promptTime);
                }
            });
        }

        //获取代理接口数据(案件列表)
        function getProxyData(){
            $scope.loadingShow = true;
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '1';
            headerInfo.pageSize = '10';
            headerInfo.sessionId = transData.sessionId;

            bizInfo.province = "";
            bizInfo.city = "";
            bizProxy.queryProxyBizData("matchCaseList",headerInfo,bizInfo,
                    function(tempData){//SUCCESS
                        if(tempData.body != "" && tempData.body != null && tempData.body != "[]"){
                            var tempCaseList = JSON.parse(tempData.body).caseInfoList;
                            console.log("案件列表=",JSON.parse(tempData.body))
                            transData.scrollCaseList = tempCaseList;
                            $timeout(function(){
                                 $scope.loadingShow = false;
                                transData.scrollCaseList = tempCaseList;
                                $scope.tempCaseData = tempCaseList;
                                swiperImage();
                                if(transData.userId == "" || transData.userId == null){
                                    autoLogin();
                                }else {
                                    queryDdVersionList();
                                }
                            },0);
                        }
                    },function(data){
                        if(transData.userId == "" || transData.userId == null){
                            autoLogin();
                        }else {
                            queryDdVersionList();
                        }
                    });
        }

        //图片滚动
    function swiperImage(){
        $timeout(function(){
            mySwiper = new Swiper('.swiper-container', {
                   // direction : 'vertical',
                   autoplay: 3000,
                   autoplayDisableOnInteraction : false,
                   loop: true
               });
        },1000);
    }

    //点击立即申请
    $scope.applyforNow = function(caseBizMode){
        if(transData.sessionId != null && transData.sessionId != ""){
            if(resultData.loginUserData.userName){
                transData.caseBizModeFormStep1=caseBizMode;
                $scope.$emit("goNext","/applyRisk");
            }else{//未完善资料用户不可申请
                FIREFLY("请先去'我的'里面完善个人信息", "","",function(){});
            }
        }else{
            $scope.$emit("goNext","/login");
        }
    }

    //立即申请债权收购
    $scope.applyBuyforNow = function(){
        if(transData.sessionId != null && transData.sessionId != ""){
                //判断是不是律师
                if(transData.userType === '1'){
                    $scope.$emit("goNext","/applyNpl");
                }else{
                   FIREFLY("对不起，您还不是律师用户", "请先在'个人信息'中升级为律师用户！","",function(){ });
                }
            }else{
                $scope.$emit("goNext","/login");
            }
    }


        var getBaseData=function (list) {
            var step=0;
            var exec=function () {
                getBaseData_query(list[step],function (next) {
                    if(next){
                        exec()
                    }
                },list[++step])
            }
            exec()
        };
        var getBaseData_query=function(name,suCallBack,next){
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = transData.sessionId;
            bizProxy.queryProxyBizData(name,headerInfo,bizInfo,function(tempData){
                    if(tempData.body != '{}'){
                        $timeout(function(){
                            console.log(name+' : ',JSON.parse(tempData.body));
                            transData.storage_baseData[name]=JSON.parse(tempData.body);
                            localStorage.setItem('storage_baseData',JSON.stringify(transData.storage_baseData));
                            suCallBack(next);
                        },0);
                    }
                },
                function(data){
                    console.log(data);
                    if(data.errorMsg){
                        // $scope.$emit("loadingChange",false);
                        // $scope.$emit("promptAlertMsg",data.errorMsg);
                        // $scope.$emit("showCollect",true);
                        // $timeout(function(){
                        //     $scope.$emit("showCollect",false);
                        // },transData.promptTime);
                    }
                });
        }
       /*if(!locationCityInfo.lat || !locationCityInfo.lon){
            map.getCurrLocation(null,function(suc){
            	console.log(suc)
                locationCityInfo.lat=JSON.parse(suc).lat;
                locationCityInfo.lon=JSON.parse(suc).lon;
                console.log(locationCityInfo.lat,'-',locationCityInfo.lon)
            },function(err){
                console.log(err)
            })
        }*/
        $scope.$apply();
    }];
});